import { EditorView } from "@codemirror/view";

// 关于如何监听Codemirrror 的内容变化，文档里面没有看到，只能使用原始办法，直接监听input事件
export default function useEditorOnchange(view: EditorView, cb: Function) {
  const editorElemet = view.contentDOM;
  //https://www.cnblogs.com/vickylinj/p/13652696.html/
  var flag = true;
  editorElemet.addEventListener("compositionstart", function () {
    flag = false;
  });
  editorElemet.addEventListener("compositionend", function () {
    flag = true;
  });
  editorElemet.addEventListener("input", function () {
    setTimeout(function () {
      if (flag) {
        cb();
        return view.state.doc.toString();
      }
    }, 60);
  });
}
